﻿using Sidvall.Data;
using Sidvall.Security;
using System.Linq;

namespace Sidvall.DataPortal
{
    public class ServerConnectionCollection : System.Collections.ObjectModel.Collection<ServerConnection>
    {
        #region Public Members

        #region AddFileStorageServerConnection

        public ServerConnection AddFileStorageServerConnection(string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName)
        {
            return AddFileStorageServerConnection(storageManagerFactoryTypeName, transactionScopeFactoryTypeName, null, null, null);
        }
        public ServerConnection AddFileStorageServerConnection(string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName, string path)
        {
            return AddFileStorageServerConnection(storageManagerFactoryTypeName, transactionScopeFactoryTypeName, path, null, null);
        }
        public ServerConnection AddFileStorageServerConnection(string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName, string path, string key,
            string description)
        {
            var item = ServerConnectionManager.GetFileStorageServerConnection(path, key, description, storageManagerFactoryTypeName, transactionScopeFactoryTypeName);
            Add(item);
            return item;
        }
        public ServerConnection AddFileStorageServerConnection(IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory)
        {
            return AddFileStorageServerConnection(storageManagerFactory, transactionScopeFactory, null, null, null);
        }
        public ServerConnection AddFileStorageServerConnection(IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory, string path)
        {
            return AddFileStorageServerConnection(storageManagerFactory, transactionScopeFactory, path, null, null);
        }
        public ServerConnection AddFileStorageServerConnection(IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory, string path, string key,
            string description)
        {
            var item = ServerConnectionManager.GetFileStorageServerConnection(path, key, description, storageManagerFactory, transactionScopeFactory);
            Add(item);
            return item;
        }

        #endregion
        #region AddSqliteConnection

        public ServerConnection AddSqliteConnection(string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName)
        {
            return AddSqliteConnection(storageManagerFactoryTypeName, transactionScopeFactoryTypeName, null, null, null);
        }
        public ServerConnection AddSqliteConnection(string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName, string filePath)
        {
            return AddSqliteConnection(storageManagerFactoryTypeName, transactionScopeFactoryTypeName, filePath, null, null);
        }
        public ServerConnection AddSqliteConnection(string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName, string filePath, string key,
            string description)
        {
            var item = ServerConnectionManager.GetSqliteConnection(filePath, key, description, storageManagerFactoryTypeName, transactionScopeFactoryTypeName);
            Add(item);
            return item;
        }
        public ServerConnection AddSqliteConnection(IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory)
        {
            return AddSqliteConnection(storageManagerFactory, transactionScopeFactory, null, null, null);
        }
        public ServerConnection AddSqliteConnection(IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory, string filePath)
        {
            return AddSqliteConnection(storageManagerFactory, transactionScopeFactory, filePath, null, null);
        }
        public ServerConnection AddSqliteConnection(IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory, string filePath, string key,
            string description)
        {
            var item = ServerConnectionManager.GetSqliteConnection(filePath, key, description, storageManagerFactory, transactionScopeFactory);
            Add(item);
            return item;
        }

        #endregion
        #region AddSqlServerConnection

        public ServerConnection AddSqlServerConnection(string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName)
        {
            return AddSqlServerConnection(storageManagerFactoryTypeName, transactionScopeFactoryTypeName, null, null, null, null);
        }
        public ServerConnection AddSqlServerConnection(string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName, string connectionString,
            string storageManagerAssembly)
        {
            return AddSqlServerConnection(storageManagerFactoryTypeName, transactionScopeFactoryTypeName, connectionString, storageManagerAssembly, null, null);
        }
        public ServerConnection AddSqlServerConnection(string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName, string connectionString,
            string storageManagerAssembly, string key, string description)
        {
            var item = ServerConnectionManager.GetSqlServerConnection(connectionString, storageManagerAssembly, key, description,
                storageManagerFactoryTypeName, transactionScopeFactoryTypeName);
            Add(item);
            return item;
        }
        public ServerConnection AddSqlServerConnection(IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory)
        {
            return AddSqlServerConnection(storageManagerFactory, transactionScopeFactory, null, null, null, null);
        }
        public ServerConnection AddSqlServerConnection(IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory, string connectionString,
            string storageManagerAssembly)
        {
            return AddSqlServerConnection(storageManagerFactory, transactionScopeFactory, connectionString, storageManagerAssembly, null, null);
        }
        public ServerConnection AddSqlServerConnection(IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory, string connectionString,
            string storageManagerAssembly, string key, string description)
        {
            var item = ServerConnectionManager.GetSqlServerConnection(connectionString, storageManagerAssembly, key, description,
                storageManagerFactory, transactionScopeFactory);
            Add(item);
            return item;
        }

        #endregion
        #region AddWebServiceConnection

        public ServerConnection AddWebServiceConnection(string address, IAuthenticationStrategy authenticationStrategy)
        {
            return AddWebServiceConnection(address, authenticationStrategy, null, null);
        }
        public ServerConnection AddWebServiceConnection(string address, IAuthenticationStrategy authenticationStrategy, string key, string description)
        {
            var item = ServerConnectionManager.GetWebServiceConnection(address, key, description, authenticationStrategy);
            Add(item);
            return item;
        }

        #endregion

        #region Contains

        public bool Contains(string key)
        {
            var item = FindServerConnectionByKey(key);
            return (item != null);
        }
        public bool Contains(ConnectionType type)
        {
            var item = FindServerConnectionByType(type);
            return (item != null);
        }

        #endregion
        #region FindServerConnectionByKey

        public ServerConnection FindServerConnectionByKey(string key)
        {
            if (!string.IsNullOrWhiteSpace(key))
            {
                return (from o in this
                        where (o.Key != null) && (o.Key.Equals(key, System.StringComparison.OrdinalIgnoreCase))
                        select o).FirstOrDefault();
            }
            return null;
        }

        #endregion
        #region FindServerConnectionByType

        public ServerConnection FindServerConnectionByType(ConnectionType type)
        {
            return (from o in this
                    where o.ConnectionTypeId == type
                    select o).FirstOrDefault();
        }

        #endregion
        #region Remove

        public bool Remove(string key)
        {
            var item = FindServerConnectionByKey(key);
            if (item != null)
                return Remove(item);
            return false;
        }
        public bool Remove(ConnectionType type)
        {
            var item = FindServerConnectionByType(type);
            if (item != null)
                return Remove(item);
            return false;
        }

        #endregion

        #endregion
    }
}
